﻿function AuthenticationController($scope, service, $routeParams, $location, $http) {
    //var ip = Request.UserHostAddress;
    //var hostname = Request.UserHostName;

    $scope.loginUser = {
        userId: "user@abc.com",
        password: "123456"
    };
    $scope.registerUser = {
        email: "",
        password: "",
        company_name: "",
        is_ogranization: true,
        location_name: "",
    };
    $scope.forgotPasswordUser = {
        email: "",
        userId: "",
        password: "",
        new_password:""
    };

    //Login action
    $scope.login = function () {
        if ($scope.loginUser) {
            $http.get(service.locationUrl + "/login/" + $scope.loginUser.userId + "/" + $scope.loginUser.password, {})
          .success(function (data) {
              $http.get(service.locationUrl + "/company/useraccess/" + data.Id, {})
             .success(function (data) {
                 service.infoUser = data[0];


                 //Should use base64 encoding to encode the key here
                 sessionStorage.setItem('loggedUser', $scope.loginUser.userId);
                 //Set infoUser to session too
                 sessionStorage.setItem('infoUser', angular.toJson(service.infoUser));


                 $location.path('/Top');

                 // post user history
                 $.ajax({
                     url: '//freegeoip.net/json/',
                     type: 'POST',
                     dataType: 'jsonp',
                     success: function (location) {
                         var ip = location.ip;
                         var hostName = window.location.host;
                         var loginTime = new Date();

                         console.log("ip " + ip);
                         console.log("host name " + hostName);
                         console.log("logintime " + loginTime);

                         var objUserHistory = {

                             UserAccessId: service.infoUser.UserAccessId,
                             LoginTime: loginTime,
                             LogoutTime: "",
                             SourceIp: ip,
                             HostName: hostName,
                             Content: "",
                             EditTarget: "",
                             EditConten: "",
                             BeforeEdit: "",
                             AfterEdit: "",
                         }
                         $http.post(
                            service.locationUrl + "/userhistory",
                            objUserHistory)
                        .success(function () { })
                        .error(function () { });
                     }
                 });

             })
             .error(function (err) {

                 console.log("error", err);
             });


          })
          .error(function (err) {

              console.log("error", err);
          });

        }
        
    }


    //Register: send email active
    $scope.register = function () {
        var registerEmail = {
            Email: $scope.registerUser.email
        };
        $.post(
            "api/Authentication/action/Register",
            registerEmail,
            function (data) {
                alert(data);
            });
    }

    //Register Confirm 
    $scope.registerConfirm = function () {
        var registerConfirm = {
            Email: $scope.registerUser.email,
            Password: $scope.registerUser.password,
            CompanyName: $scope.registerUser.company_name,
            IsOgranization: $scope.registerUser,
            LocationName: $scope.registerUser.location_name
        }
        $.post(
            "api/Authentication/action/RegisterCompleteInformation",
            registerConfirm,
            function (data) {
                alert(data);
            });
    }

    //Forgot password
    $scope.forgotPassword = function () {
        var forgotPassword = {
            UserId: $scope.forgotPasswordUser.userId,
            Email: $scope.forgotPasswordUser.email
        };

        $.post(
            "api/Authentication/action/ForgotPassword",
            forgotPassword,
            function (data) {
                alert(data);
            });
    }

    //Reset password
    $scope.resetPassword = function () {
        var resetPassword = {
            NewPassword: $scope.forgotPasswordUser.new_password
        };

        $.post(
            "api/Authentication/action/ResetPassword",
            resetPassword,
            function (data) {
                alert(data);
            });
    }


    $scope.test = function () {
        var hostName = window.location.host;
        var loginTime = new Date();

        var objUserHistory = {
            UserAccessId: 1,
            LoginTime: loginTime,
            LogoutTime: "",
            SourceIp: "192.168.1.70",
            HostName: hostName,
            Content: "",
            EditTarget: "",
            EditConten: "",
            BeforeEdit: "",
            AfterEdit: "",
        }
        $http.put(
           service.locationUrl + "/userhistory/1",
           objUserHistory)
       .success(function (data) {
           console.log("success : ", data);
       })
       .error(function (err) {
           console.log("err : ", err);
       });
    }
}

